package controler;

import dao.UsuarioDAO;
import java.awt.Color;
import java.awt.Toolkit;
import java.util.List;
import model.Usuario;
import util.Relogio;
import view.GUI_desktop;
import view.GUI_login;

public class Controler_Acesso {
    
    public GUI_login gui = new GUI_login();
    public GUI_desktop desk = new GUI_desktop();
    
    public void initLogin(){
        UsuarioDAO dao = new UsuarioDAO();
        List<Usuario> lista;
        lista = dao.findAll();
        for(Usuario u : lista){
            if(u.isAtivo() == true){
                this.gui.boxUser.addItem(u.getUser());
            }
        }
        gui.control = this;
        gui.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icon/32x32/lock.png")));
        gui.setVisible(true);
    }
    
    public void autenticUser(String user, String senha){
        Usuario u = new UsuarioDAO().findByUser(user);
        if(u.getSenha().equals(senha)){
            this.initDesk(u);
            gui.dispose();
        }else{
            gui.lblMsg.setText("Dados inválidos");
            gui.lblMsg.setForeground(Color.red);
        }
    }
    
    public void initDesk(Usuario u){
        
        desk.control = this;
        desk.lblUsuario.setText(u.getNome());
        this.setDate();
        gui.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icon/32x32/world.png")));
//        gui.setExtendedState(MAXIMIZED_BOTH);
        
        if(u.isAcessoCadastro() == true){
           
        }
        if(u.isAcessoFinanceiro() == true){
            
        }
        if(u.isAcessoManutencao() == true){
           
        }
        
        desk.setVisible(true);
        
    }
    
    private void setDate(){
        new Thread() {
            @Override
            public void run() {
                for(;;){
                    Relogio r = new Relogio();
                    desk.lblData.setText(r.getFullDate());
                }
            }
        }.start();
    }
    
}//CLASS
